package cn.huibangbang.easyexcel.listener;

import cn.huibangbang.easyexcel.domain.Student;
import cn.huibangbang.easyexcel.service.StudentService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhang
 * @version 1.0
 * @description TODO
 * @date 2023/6/28 0028
 */
@RequiredArgsConstructor
@Component
@Scope("prototype")
public class WebStudentListener extends AnalysisEventListener<Student> {

    private final StudentService studentService;

    List<Student> students = new ArrayList<>();

    @Override
    public void invoke(Student student, AnalysisContext context) {
        students.add(student);
        if (students.size() % 5 == 0) {
            studentService.readExcel(students);
            students.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

}
